package com.gqs.bookstore.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gqs.bookstore.dao.BaseDao;
import com.gqs.bookstore.dao.BookDao;
import com.gqs.bookstore.vo.Book;
import com.gqs.bookstore.vo.PageBean;

public class BookDaoImpl extends BaseDao implements BookDao {
	/**
	 * 分页的方法
	 * @throws SQLException 
	 */
	@Override
	public PageBean<Book> findByPagination(Long catalogId, Long pageIndex,
			Long pageSize) throws SQLException {
		PageBean<Book> pb = new PageBean<Book>();
		//填充PageBean对象的pageIndex和pageSize属性
		pb.setPageIndex(pageIndex);
		pb.setPageSize(pageSize);
		
		//注意这里的condition前后都要用空格隔开----这是一条sql语句，要注意
		String condition = " where catalog_id=? and status=1 ";
		
		//填充PageBean对象的total属性
		String sqlTotal = "select count(*) total from books"+condition;//total是别名，as省略
		Object[] params = {catalogId};
		List<Map<String, Object>> totalResult = super.executeQuery(sqlTotal, params);
		Long total = convertTotal(totalResult);//系统自动抽取提成这个方法
		pb.setTotal(total);
		
		//填充PageBean对象的List<P>属性
		//用 asc   表示按正序排序(即：从小到大排序)
		//用 desc  表示按倒序排序(即：从大到小排序)
		String sqlPageBean="select * from books"+condition+"order by publish_date desc limit ?,?";
		Object[] params1 = {catalogId, (pageIndex-1)*pageSize, pageSize};
		List<Map<String, Object>> listPageBean =super.executeQuery(sqlPageBean, params1);
		//通过查询数据库得到每页的数据的列表,在通过convertBook函数转换成普通的list
		List<Book> books = convertBook(listPageBean);//系统自动抽取提成这个方法
		pb.setData(books);
		
		super.closeConnection();
		return pb;
	}
	
	
	//eclipse抽取一些代码做成方法以供调用
	//总条数
	private Long convertTotal(List<Map<String, Object>> totalResult) {
		Long total = null;
		if(!totalResult.isEmpty()){
			Map<String, Object> recordFirst = totalResult.get(0);
			if(recordFirst!=null){
				 total = (Long) recordFirst.get("total");
				}
		}
		return total;
	}
	
	
	//eclipse抽取一些代码做成方法以供调用
	//这个方法作用：保存每页的数据的列表
	private List<Book> convertBook(List<Map<String, Object>> listPageBean) {
		List<Book> books = null;
		if(!listPageBean.isEmpty()){
			//这里用了另外一种方式foreach来遍历list，而CatalogDaoImpl.java里面的findAll()方法遍历用的是迭代器
			//但这种foreach来遍历方式简单[其实foreach循环在底层把迭代器封装了]
			books = new ArrayList<Book>();
			for (Map<String, Object> recordPageBean : listPageBean) {
				Book book = convertToBook(recordPageBean);
				books.add(book);
			}
		}
		return books;
	}

	
	//eclipse在convertBook方法里抽取一些代码做成方法以供调用
	//这个方法作用：仅仅只把从map里面查到的每一行记录转换成每一个对应的book对象
	private Book convertToBook(Map<String, Object> recordPageBean) {
		Book book = new Book();
		book.setId((Long) recordPageBean.get("id"));
		book.setName((String) recordPageBean.get("name"));
		book.setAuthor((String) recordPageBean.get("author"));
		book.setPublisher((String) recordPageBean.get("publisher"));
		book.setPic((String) recordPageBean.get("pic"));
		//注意Double数据应该这样进行处理
		book.setPrice(((BigDecimal)recordPageBean.get("price")).doubleValue());
		//book.setCatalogId((Long) recordPageBean.get("catalog_id"));与底下等价
		book.setCatalogId(Long.valueOf((Integer)recordPageBean.get("catalog_id")));
		book.setPublishDate((Date) recordPageBean.get("publish_date"));
		book.setUpdateAt((Date) recordPageBean.get("update_at"));
		book.setUpdateAt((Date) recordPageBean.get("create_at"));
		return book;
	}


	@Override
	public Book findById(Long id) throws SQLException {
		Book book = null;
		String sql = "select * from books where id=?";
		Object[] params = {id};
		List<Map<String, Object>> list = super.executeQuery(sql, params);
		if(!list.isEmpty()){
			Map<String, Object> record = list.get(0);
			if(record!=null){
				 book = convertToBook(record);
			}
		}
		super.closeConnection();
		return book;
	}
	
	
}
